package com.testing.redboy.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.testing.redboy.R;
import com.testing.redboy.bean.TuiBean;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

public class Tui_Fragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private ExpandableListView exp;


    public Tui_Fragment() {
    }

    public static Tui_Fragment newInstance(String param1, String param2) {
        Tui_Fragment fragment = new Tui_Fragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_tui_, container, false);

        exp = (ExpandableListView) v.findViewById(R.id.exp);

        setData();

        exp.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

                Toast.makeText(getActivity(), "第" + (childPosition + 1) + "子条目", Toast.LENGTH_SHORT).show();

                return true;
            }
        });

        TextView back = (TextView) v.findViewById(R.id.limit_back);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().onBackPressed();
            }
        });

        return v;
    }

    public void setData() {
        String u = "http://120.27.126.103:8080/ECServer_D/brand";
        RequestParams params = new RequestParams(u);
        x.http().get(params, new Callback.CommonCallback<String>() {

            private List<TuiBean.BrandBean> list;

            @Override
            public void onSuccess(String result) {
                Gson g = new Gson();
                TuiBean tb = g.fromJson(result, TuiBean.class);
                list = tb.brand;
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                Mybs mybs = new Mybs(getActivity(), list);
                exp.setAdapter(mybs);
                for (int i = 0; i < mybs.getGroupCount(); i++) {
                    exp.expandGroup(i);
                }
            }
        });
    }

    class Mybs extends BaseExpandableListAdapter {

        Context context;
        List<TuiBean.BrandBean> list;

        public Mybs(Context context, List<TuiBean.BrandBean> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getGroupCount() {
            return list.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return list.get(groupPosition).value.size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return list.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return list.get(groupPosition).value.get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

            convertView = View.inflate(context, R.layout.tui_dad, null);

            TextView brand_class = (TextView) convertView.findViewById(R.id.brand_class);

            brand_class.setText(list.get(groupPosition).key);

            return convertView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

            convertView = View.inflate(context, R.layout.tui_son, null);

            ImageView m1 = (ImageView) convertView.findViewById(R.id.m1);

            List<TuiBean.BrandBean.ValueBean> arr = list.get(groupPosition).value;

            String mu = "http://120.27.126.103:8080/ECServer_D/";

            x.image().bind(m1, mu + arr.get(childPosition).pic);

            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }

}
